package com.btshop.goods.service.impl;
import com.btshop.goods.mapper.CategoryMapper;
import com.btshop.goods.pojo.Category;
import com.btshop.goods.pojo.CategoryExample;
import com.btshop.goods.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
import com.util.IdWorker;

/**
 * @ClassName: CategoryServiceImpl
 * @description: Category业务层接口实现类
 * @author: 李东
 * @Date: 2020-7-20
 */
@Service
public class CategoryServiceImpl implements CategoryService {


    @Autowired
    private IdWorker idWorker;
    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * 根据父节点id查询子分类
     * @param parentId
     * @return
     */
    @Override
    public List<Category> findByParentId(Long parentId) {
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        return categoryMapper.selectByExample(example);
    }

    /**
     * Category条件+分页查询
     * @param category 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Category> findPage(Category category, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        CategoryExample example = createExample(category);
        //执行搜索
        return new PageInfo<Category>(categoryMapper.selectByExample(example));
    }

    /**
     * Category分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Category> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Category>(this.buildSelectAll());
    }

    /**
     * Category条件查询
     * @param category
     * @return
     */
    @Override
    public List<Category> findList(Category category){
        //构建查询条件
        CategoryExample example = createExample(category);
        //根据构建的条件查询数据
        return categoryMapper.selectByExample(example);
    }


    /**
     * Category构建查询对象
     * @param category
     * @return
     */
    public CategoryExample createExample(Category category){
        CategoryExample example=new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        if(category!=null){
            // 分类ID
            if(!StringUtils.isEmpty(category.getId())){
                    criteria.andIdEqualTo(category.getId());
            }
            // 分类名称
            if(!StringUtils.isEmpty(category.getName())){
                    criteria.andNameLike("%"+category.getName()+"%");
            }
            // 商品数量
            if(!StringUtils.isEmpty(category.getGoodsNum())){
                    criteria.andGoodsNumEqualTo(category.getGoodsNum());
            }
            // 是否显示
            if(!StringUtils.isEmpty(category.getIsShow())){
                    criteria.andIsShowEqualTo(category.getIsShow());
            }
            // 是否导航
            if(!StringUtils.isEmpty(category.getIsMenu())){
                    criteria.andIsMenuEqualTo(category.getIsMenu());
            }
            // 排序
            if(!StringUtils.isEmpty(category.getSeq())){
                    criteria.andSeqEqualTo(category.getSeq());
            }
            // 上级ID
            if(!StringUtils.isEmpty(category.getParentId())){
                    criteria.andParentIdEqualTo(category.getParentId());
            }
            // 模板ID
            if(!StringUtils.isEmpty(category.getTemplateId())){
                    criteria.andTemplateIdEqualTo(category.getTemplateId());
            }
            // 创建时间
            if(!StringUtils.isEmpty(category.getCreateTime())){
                    criteria.andCreateTimeEqualTo(category.getCreateTime());
            }
            // 更新时间
            if(!StringUtils.isEmpty(category.getUpdateTime())){
                    criteria.andUpdateTimeEqualTo(category.getUpdateTime());
            }
            // 乐观锁版本号
            if(!StringUtils.isEmpty(category.getOptimistic())){
                    criteria.andOptimisticEqualTo(category.getOptimistic());
            }
            // 备注字段
            if(!StringUtils.isEmpty(category.getRemark())){
                    criteria.andRemarkEqualTo(category.getRemark());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void delete(Long id){
        categoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Category
     * @param category
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void update(Category category){
        categoryMapper.updateByPrimaryKey(category);
    }

    /**
     * 增加Category
     * @param category
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void add(Category category){
        //设置主键值
        category.setId(idWorker.nextId());
        categoryMapper.insert(category);
    }

    /**
     * 根据ID查询Category
     * @param id
     * @return
     */
    @Override
    public Category findById(Long id){
        return  categoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Category全部数据
     * @return
     */
    @Override
    public List<Category> findAll() {
        return this.buildSelectAll();
    }

    public List<Category> buildSelectAll(){
        return categoryMapper.selectByExample(null);
    }
}
